package fr.enseirbmatmeca.apms.tapestry.components;

import fr.enseirbmatmeca.apms.spring.model.project.Project;
import fr.enseirbmatmeca.apms.spring.model.user.User;
import fr.enseirbmatmeca.apms.spring.service.ProjectManager;
import fr.enseirbmatmeca.apms.spring.service.UserManager;
import fr.enseirbmatmeca.apms.tapestry.pages.Security;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

/**
 *
 * @author Fabien Fleurey
 */
public class ProjectList extends Security {

    private Log logger = LogFactory.getLog(ProjectList.class);
   
    @Inject 
    private ProjectManager projectManager;
    @Inject 
    private UserManager userManager;
    @Property
    private Project project;
    
    private List<Project> projects;
    
    public ProjectList() {
        User user = userManager.getUser(getUsername());
        if (user == null) {
            throw new IllegalStateException(getUsername() + " is not registered in the database.");
        }
        projects = projectManager.getProjects(user);
    }    
    
    public List<Project> getProjects() {
        return projects;
    }
}
